package cn.change.basic.controller;

import cn.change.basic.dto.LoginDto;
import cn.change.basic.exception.BusinssException;
import cn.change.basic.service.ILoginService;
import cn.change.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    /**
     * 账号密码登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        System.out.println(dto);
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }


    /*wechat 微信第三方扫码登录*/
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            return  loginService.loginWechat(param);
        } catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    /**
     * 微信绑定
     * @param param
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String, Object> map =  loginService.binder(param);
            return  AjaxResult.me().setResultObj(map);
        } catch (BusinssException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }
}
